$(document).ready(function(){
	
	$('input:checkbox').click(function(){
		$(this).parent().parent().toggleClass('ui-state-highlight');
	});

	// Uploadify's options
	$('#update-file-upload').uploadify({
		'uploader'  : 'inc/uploadify.swf',
		'script'    : 'process/upload_docs.php',
		'cancelImg' : 'css/img/cancel.png',
		'buttonImg'	: 'css/img/folder_add.png',
		'folder'    : 'data/docs',
		'auto'      : false,
		'multi'		: true,
		'wmode'		: 'transparent',
		'height'	: '16',
		'scriptData': { 'otype':'update', 'id':$('input[name=update-updateId]').val() },
		'onAllComplete'	: function(event,data){
			location.reload();
		}
	});
	
	// Case: deleting an activity's document
	$('a.delete-doc').click(function(){
		var e = $(this);
		$.ajax({
			type:	'POST',
			url:	'process/document_delete.php',
			data:	'id='+e.attr('href'),
			success: function(msg){
				if (msg != 'OK')
					alert(msg);
				else
					e.parent().parent().remove();
			}
		});
		return false;
	});
	
	// Case: editing update's case
	$('input[name=update-supportcase]').click(function(){
		var e = $(this);
		e.addClass('loading');
		$.ajax({
			type:	'POST',
			url:	'ajax/load_supportcases.php',
			data:	'id='+$('input[name=update-supportcase]').val(),
			success: function(msg) {
				var select = $('div#select_supportcase');
				e.removeClass('loading');
				select.html(msg);
				select.dialog({
					resizable: true,
					height: 500,
					width: 500,
					modal: true,
					buttons: {
						'Sélectionner': function(){
							var checked = $('input:radio:checked');
							if (checked.length <= 0)
								$('input[name=update-contact]').val('');
							else
								$('input[name=update-contact]').val(checked.first().val());
							$(this).dialog('close');
						},
						'Annuler': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
	
	// Case: editing update's owner
	$('input[name=update-owner]').click(function(){
		var e = $(this);
		e.addClass('loading');
		$.ajax({
			type:	'POST',
			url:	'ajax/load_crmusers.php',
			data:	'id='+$('input[name=update-ownerId]').val(),
			success: function(msg) {
				var select = $('div#select_crmuser');
				e.removeClass('loading');
				select.html(msg);
				select.dialog({
					resizable: true,
					height: 500,
					width: 500,
					modal: true,
					buttons: {
						'Sélectionner': function(){
							var checked = $('input:radio:checked');
							if (checked.length <= 0) {
								$('input[name=update-owner]').val('');
								$('input[name=update-ownerId]').val('');
							} else {
								$('input[name=update-owner]').val($('span#crmuser-'+checked.first().val()).html());
								$('input[name=update-ownerId]').val(checked.first().val());
							}
							$(this).dialog('close');
						},
						'Annuler': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
	
	// Case: submitting a new (or edited) update
	$('a#form-update').click(function(){
		
		// Getting inputs
		var operation = $('input[name=update-operation]'),
			id = $('input[name=update-updateId]'),
			supportcase = $('input[name=update-supportcase]'),
			content = $('textarea[name=update-content]'),
			owner = $('input[name=update-ownerId]'),
			allFields = $([]).add(content);
		
		// Checking rights
		if (check_credentials('update_'+operation.val()) != 'yes'){
			alert('Vous n\'avez pas le droit d\'effectuer cette opération !');
			return false;
		}
		
		// Checking form validity
		allFields.removeClass('ui-state-error');
		var valid = true;
		valid = valid && check_length(content, 'Description', 1, 65535);
		valid = valid && check_length(owner, 'Propriétaire', 1, 10);
		
		// We have errors
		if (!valid)
			return false;

		// No error: AJAX processing
		showLoading();
		$.ajax({
			type:	'POST',
			url:	'process/update_'+operation.val()+'.php',
			data:	'id='+id.val()+'&supportcase='+supportcase.val()+'&content='+htmlEncode(content.val())+'&owner='+owner.val(),
			success: function(msg){
				addLoading(100);
				history.back();
			}
		});
		return false;
	});
});